package com.wyh.controller;


import com.wyh.common.annotation.Log;
import com.wyh.common.annotation.RateLimiter;
import com.wyh.common.enums.BusinessType;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.ExcelUtils;
import com.wyh.entity.AdminArea;
import com.wyh.request.AdminAreaRequest;
import com.wyh.service.IAdminAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 地区管理控制层
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "地区管理接口管理",tags = "地区管理接口管理")
@RequestMapping("/admin/adminArea" )
public class AdminAreaController {

    private final IAdminAreaService iAdminAreaService;

    /**
     * 分页查询地区管理列表
     */
    @ApiOperation(value = "分页查询地区管理列表")
    @PostMapping("/pageList")
    public R<PageInfo<AdminArea>> pageList(@RequestBody AdminAreaRequest adminAreaRequest) {
        return R.success(iAdminAreaService.pageList(adminAreaRequest));
    }

    /**
     * 获取地区管理详细信息
     */
    @ApiOperation(value = "获取地区管理详细信息")
    @GetMapping(value = "/query/{id}")
    public R<AdminArea> detail(@PathVariable("id") Integer id) {
        return R.success(iAdminAreaService.getById(id));
    }

    /**
     * 新增地区管理
     */
    @Log(title = "新增地区管理", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增地区管理")
    @PostMapping("/add")
    public R<Void> add(@RequestBody AdminAreaRequest adminAreaRequest) {
        iAdminAreaService.add(adminAreaRequest);
        return R.success();
    }

    /**
     * 修改地区管理
     */
    @Log(title = "修改地区管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改地区管理")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody AdminAreaRequest adminAreaRequest) {
        iAdminAreaService.update(adminAreaRequest);
        return R.success();
    }

    /**
     * 删除地区管理
     */
    @Log(title = "删除地区管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除地区管理")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids) {
        iAdminAreaService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出地区管理
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody AdminAreaRequest adminAreaRequest) throws IOException {
        List<AdminArea> list = iAdminAreaService.list(adminAreaRequest);
        ExcelUtils.exportExcel(list, AdminArea.class, "地区管理信息", response);
    }
}
